//
//  PushListViewController.m
//  商城
//
//  Created by runone02 on 2018/8/31.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "PushListViewController.h"
#import "DemoTableViewController.h"
@interface PushListViewController ()<MYTreeTableViewControllerDelegate>
@property (nonatomic, strong) NSArray <MYTreeItem *>*checkItems;  // 所选择的 items 传出来的数据
/** xians*/
@property (nonatomic,strong) UILabel *showLabel;
@end

@implementation PushListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitle:@"进入列表" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(self.view);
    }];
    
    self.showLabel = [[UILabel alloc]init];
    self.showLabel.textColor = [UIColor blackColor];
    self.showLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.showLabel];
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(btn.mas_top).with.offset(-20);
        make.left.right.mas_equalTo(self.view);
        make.centerX.mas_equalTo(self.view);
    }];
}

- (void)btnClick:(UIButton *)btn{
    // 这里的个性化设置也可以移到 DemoTableViewController 中的 viewDidLoad 执行
    DemoTableViewController *vc = [[DemoTableViewController alloc] init];
    vc.delegate = self;
//    vc.isSingleCheck = YES;
    vc.isShowArrow = YES;
    vc.isShowCheck = YES;
    vc.isShowLevelColor = YES;
    vc.isShowSearchBar = YES;
    vc.checkItemIds = [self getItemIds];
    vc.isShowExpandAnimation = YES;
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSArray *)getItemIds {
    
    // demo 中的逻辑判断没有处理的那么严谨，多选切换成单选后，当前多选选择的数据全部清空
    if ( self.checkItems.count > 1) {
        self.checkItems = [NSArray array];
        self.showLabel.text = @"已选择了 0 个 items";
    }
    
    NSMutableArray *itemIds = [NSMutableArray array];
    for (MYTreeItem *item in self.checkItems) {
        [itemIds addObject:item.ID];
    }
    return itemIds.copy;
}


#pragma mark - MYTreeTableViewControllerDelegate

- (void)tableViewController:(MYTreeTableViewController *)tableViewController checkItems:(NSArray<MYTreeItem *> *)items {
    
    self.checkItems = items;
    self.showLabel.text = [NSString stringWithFormat:@"已选择了 %lu 个 items", (unsigned long)items.count];
}

@end
